package com.xinghou.platform.dao.wzk.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.wzk.MeijiimgDAO;
import com.xinghou.platform.model.wzk.Meijiimg;
import com.xinghou.platform.model.wzk.Showcardimg;

@Repository
public class MeijiimgDAOImpl implements MeijiimgDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public Meijiimg getMeijiimgById(String id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT id, mjid, imgpath FROM wzk.meijiimg  where id = ?";
		Meijiimg meijiimg = jdbcTemplate.queryForObject(sql, new Object[] { id }, new MeijiimgMapper());
		CustomerContextHolder.clearCustomerType();
		return meijiimg;
	}

	@Override
	public void updateMeijiimg(Meijiimg meijiimg) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE wzk.meijiimg SET mjid = ?, imgpath = ?")
		.append(" where id=?");
		
       Object[] params = {meijiimg.getMjid(),meijiimg.getImgpath(),meijiimg.getId()
     };

    CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
    int ret = jdbcTemplate.update(strSQL.toString(),params);
    CustomerContextHolder.clearCustomerType();
		
	}
	
	public List<Meijiimg> getShowcardimgById(int mjid) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT id, mjid, imgpath FROM wzk.meijiimg  where mjid = ?";
		List<Meijiimg> meijiimgs = jdbcTemplate.query(sql,new Object[] {mjid }, new MeijiimgMapper());
		CustomerContextHolder.clearCustomerType();
		return meijiimgs;
	}
	
	private static final class MeijiimgMapper implements RowMapper<Meijiimg> {
		public Meijiimg mapRow(ResultSet rs, int rowNum) throws SQLException {
			Meijiimg meijiimg = new Meijiimg();
			meijiimg.setId(rs.getInt("id"));
			meijiimg.setImgpath(rs.getString("imgpath"));
			meijiimg.setMjid(rs.getInt("mjid"));
			return meijiimg;
		}
	}
	
}
